package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.ShowEmployeeDao;
import com.clqteam.model.Employee;
import com.clqteam.util.JDBCutil;

public class ShowEmployeeDaoImpl implements ShowEmployeeDao{

	@Override
	public List<Employee> getEmployeeInfo(int page, int rows) {
		 List<Employee> emp=new ArrayList<Employee>();
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT s_employee.eId,s_employee.eAccount,s_employee.ePsw,s_employee.eName,s_role.rName");
			sql.append(" FROM s_employee");
			sql.append(" INNER JOIN  s_role");
			sql.append(" ON s_employee.rId=s_role.rId");
			sql.append(" LIMIT ?,?");
			Connection con=JDBCutil.getConnection();
			try {
				 PreparedStatement pst=con.prepareStatement(sql.toString());
				 pst.setInt(1,(page-1)*rows);
				 pst.setInt(2, rows);
				 ResultSet rs=pst.executeQuery();
				 while(rs.next()){
					 Employee emp1=new Employee();
					//s的set方法是通过s对象得到的，rs的get方法中的参数是数据库中的列名
					 emp1.seteId(rs.getInt("eId"));
					 emp1.seteAccount(rs.getString("eAccount"));
					 emp1.setePsw(rs.getString("ePsw"));
					 emp1.seteName(rs.getString("eName"));
					 emp1.setrName(rs.getString("rName"));
					 emp.add(emp1);
				 }
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return emp;
		
	}

	@Override
	public int getEmployeeCountByeId() {
		int count=0;
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT COUNT(eId)as ecount FROM s_employee");
		Connection con=JDBCutil.getConnection();
		try {
			PreparedStatement pst=con.prepareStatement(sql.toString());
			ResultSet rs=pst.executeQuery();
			if(rs.next()){
			count=rs.getInt("ecount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

}
